# Contributor: Celeste <cielesti@protonmail.com>
# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=lemmy
pkgver=0.19.3
# this needs to be synced with lemmy-ui
_translations_commit=c3e61706e2ff7977db83e964e3010f72cad5e408
pkgrel=1
pkgdesc="Link aggregator and forum for the Fediverse - Backend server"
url="https://join-lemmy.org/"
# armhf, armv7: fatal runtime error while compiling lemmy_server crate
# ppc64le, riscv64, s390x: ring crate
# x86: follow lemmy-ui aport
arch="aarch64 x86_64"
license="AGPL-3.0-only"
makedepends="
	cargo
	cargo-auditable
	libpq-dev
	openssl-dev
	"
install="$pkgname.pre-install $pkgname-localdb.post-install"
pkgusers="lemmy"
pkggroups="lemmy"
subpackages="$pkgname-openrc $pkgname-localdb::noarch"
source="https://github.com/LemmyNet/lemmy/archive/$pkgver/lemmy-$pkgver.tar.gz
	https://github.com/LemmyNet/lemmy-translations/archive/$_translations_commit/lemmy-translations-$pkgver.tar.gz
	fix-db-init-script.patch
	lemmy.initd
	"
options="!check" # fails to load settings file as it is not found

# html2md required by lemmy_apub fails to build with panic=abort
export CARGO_PROFILE_RELEASE_PANIC="unwind"

prepare() {
	default_prepare

	rmdir -v crates/utils/translations
	ln -sv "$srcdir/lemmy-translations-$_translations_commit" \
		crates/utils/translations

	sed -i "s/unknown version/$pkgver/" crates/utils/src/version.rs
	cargo fetch --target="$CTARGET" --locked
}

build() {
	cargo auditable build --frozen --release
}

check() {
	cargo test --frozen
}

package() {
	install -Dm755 target/release/lemmy_server -t "$pkgdir"/usr/bin
	install -Dm640 -g lemmy config/defaults.hjson \
		"$pkgdir"/etc/lemmy/lemmy.hjson

	install -Dm755 scripts/db-init.sh -t "$pkgdir"/usr/lib/$pkgname
	install -Dm755 "$srcdir"/lemmy.initd "$pkgdir"/etc/init.d/lemmy
}

localdb() {
	pkgdesc="Use $pkgname with a local PostgreSQL server"
	install_if="$pkgname=$pkgver-r$pkgrel postgresql"
	depends="$pkgname=$pkgver-r$pkgrel postgresql-contrib"

	amove usr/lib/$pkgname/db-init.sh
}

sha512sums="
e050fafafad45a3354460bb706387e609187e3894de9ac6c93f26f171f48dbefadc3e0d816f29efdde092dc7454a47e5c3b1786443e89763e31e7ce044a56f09  lemmy-0.19.3.tar.gz
8f3ba3a343e328ee2a5ffeed4f1e6feb7ea580620f004c8f2e9fc8cf00e647e6a7ed5a9c5e68d9bce854819357ac56a9d35b667acb9925701063f0451014f075  lemmy-translations-0.19.3.tar.gz
fcfb3881abf8ee671bb55e47c71fef8ef1328a04b3278fd9602478260b0de0bdb55b27c1f6127e9729a65e3fc05cbcd245bd1f98b93ea307755ff748a68d770d  fix-db-init-script.patch
7b4cf4242950f48914fbbb49de932a10f793ae3bb14ac3d2b2430edef1cd76cfc6d0228c3041fb09313b00547068fddbd36ed9e74edc330924a6f55732698bf2  lemmy.initd
"
